package com.yingside.gui.book;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 这个类主要是对book.txt的操作
 */

public class BookManagerImpl implements IBookManager {

    private static final String PATH = "images/book.txt";

    @Override
    public List<Book> list() {
        List<Book> list = new ArrayList<>();
        FileReader reader = null;
        BufferedReader br = null;
        try {
            reader = new FileReader(PATH);
            br = new BufferedReader(reader);

            String str = null;

            while((str = br.readLine()) != null){

                if(str == null || str.trim().equals("")){
                    continue;
                }

                String[] strs = str.split("\\s+");

                Book b = new Book();
                b.setId(Integer.parseInt(strs[0]));
                b.setBookName(strs[1]);
                b.setAuthor(strs[2]);
                b.setPrice(Double.parseDouble(strs[3]));
                list.add(b);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Book get(String bookName) {
        Book book = null;
        List<Book> list = list();


        for (int i = 0; i < list.size(); i++) {
            if(bookName.equals(list.get(i).getBookName())){
                book = list.get(i);
                break;
            }
        }
        return book;

    }

    public Book get(int id){
        Book book = null;
        List<Book> list = list();


        for (int i = 0; i < list.size(); i++) {
            if(id == list.get(i).getId()){
                book = list.get(i);
                break;
            }
        }
        return book;
    }

    @Override
    public boolean add(Book book) {
        if(book == null){
            return false;
        }

        List<Book> list = list();
        list.add(book);

        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new BufferedWriter(new FileWriter(PATH)));
            String str = "";
            for (int i = 0; i < list.size(); i++) {
                Book b = list.get(i);
                str = b.getId() + " " + b.getBookName() + " " + b.getAuthor() + " " + b.getPrice();
                pw.println(str);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            pw.close();
        }

        return true;
    }

    @Override
    public boolean delete(String bookName) {
        return false;
    }

    @Override
    public boolean delete(int id) {

        if(id <= 0){
            return false;
        }
        //读取原本的book.txt文件
        List<Book> list = list();

        //判断id，并进行删除
        //注意，这只是在内存集合中删除了数据
        for (int i = 0; i < list.size(); i++) {
            if(id == list.get(i).getId()){
                list.remove(i);
                break;
            }
        }

        //将已经删除后的List数据在写回文件
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new BufferedWriter(new FileWriter(PATH)));
            String str = "";
            for (int i = 0; i < list.size(); i++) {
                Book b = list.get(i);
                str = b.getId() + " " + b.getBookName() + " " + b.getAuthor() + " " + b.getPrice();
                pw.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            pw.close();
        }
        return true;
    }

    @Override
    public boolean update(Book book) {
        List<Book> list = list();
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId() == book.getId()){
                index = i;
                break;
            }
        }

        if(index != -1){
            Book b = list.get(index);
            b.setPrice(book.getPrice());
            b.setAuthor(book.getAuthor());
            b.setBookName(book.getBookName());
            PrintWriter pw = null;
            try {
                pw = new PrintWriter(new BufferedWriter(new FileWriter(PATH)));
                String str = "";
                for (int i = 0; i < list.size(); i++) {
                    Book b2 = list.get(i);
                    str = b2.getId() + " " + b2.getBookName() + " " + b2.getAuthor() + " " + b2.getPrice();
                    pw.println(str);
                }


            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                pw.close();
            }
            return true;
        }


        return false;
    }


}
